package net.cyue.web.easyquery.core.db.data;

public class QueryPageResult {

    private final int page;
    private final int pageSize;
    private final int pageCount;
    private final long total;

    public QueryPageResult(int page, int pageSize, Long total) {
        this.page = Math.max(page, 1);
        this.pageSize = Math.max(pageSize, 1);
        if (total == null) {
            this.total = 0;
        } else {
            this.total = total;
        }
        this.pageCount = this.calculatePageCount(this.total);
    }

    public static QueryPageResult fromPage(QueryPage page, Long total) {
        return new QueryPageResult(page.getPage(), page.getPageSize(), total);
    }

    private int calculatePageCount(long total) {
        if (total > 0) {
            return (int) Math.ceil((double) total / (double) this.pageSize);
        } else {
            return 1;
        }
    }

    public int getPage() {
        return this.page;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public int getPageCount() {
        return this.pageCount;
    }

    public long getTotal() {
        return this.total;
    }
}
